package com.yiwei.jmx.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.RetrieveUsernameBean;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.Dialog;
import com.yiwei.jmx.util.ImageUtil;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.Spans;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.UI;
import com.yiwei.jmx.util.Util;
import com.yiwei.jmx.view.V19FrameLayout;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3XueXinFindByAccountActivity extends BaseActivity {

    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.btn_wechat)
    ImageView btnWechat;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_idno)
    TextView txtIdno;
    @BindView(R.id.edit_idno)
    EditText editIdno;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.txt_code)
    TextView txtCode;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.img_code)
    ImageView imgCode;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private Unbinder unbinder;
    private int x30;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xuexin_find_account_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private Spanned setLeftTextData(String text) {
        return Spans.with(this).font("*", x30, 0xffFE5A59).
                font(text, x30, 0xff27293E).
                build();
    }

    @OnTextChanged({R.id.edit_idno, R.id.edit_name, R.id.edit_code})
    void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(UI.toString(editIdno)) && !TextUtils.isEmpty(UI.toString(editName)) && !TextUtils.isEmpty(UI.toString(editCode))) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }

    private void init() {
        x30 = getResources().getDimensionPixelSize(R.dimen.x30);
        txtIdno.setText(setLeftTextData("证件号码"));
        txtName.setText(setLeftTextData("姓名"));
        txtCode.setText(setLeftTextData("图片验证码"));
        txtTitle.setText("找回用户名");
        getChsiImgCodeInfo();
    }


    //
    public void getRetrieveUsername(String idno, String realname, String captcha) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        RetrieveUsernameBean bean = new RetrieveUsernameBean();
        bean.captcha = captcha;
        bean.idno = idno;
        bean.realname = realname;
        Api.api_service.getRetrieveUsername(bean).compose(RxLifeUtil.NocheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (result.getStatus() == 1) {
                        if (result.data.username != null && result.data.username.size() != 0) {
                            String name = result.data.username.get(0);
                            Dialog.showFindUsername(this, name, new Dialog.DialogDefaultClickListener() {
                                @Override
                                public void confirm() {
                                    Util.startActivity(V3XueXinFindByAccountActivity.this, V3XueXinForgetpassActivity.class);
                                    finish();
                                }

                                @Override
                                public void cancel() {
//                                EventBus.getDefault().post(new FindXueXinUsernameEvent());
//                                UserGlobal.setFindXueXinUsername(result.data.username);
                                    finish();
                                }
                            });
                        }

                    } else {
                        ToastUtil.makeText(this, result.getMsg());
                        getChsiImgCodeInfo();
                    }
                });
    }

    public void getChsiImgCodeInfo() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        Api.api_service.getXueXinCode().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(model -> {
                    String imgStr = "";
                    String captcha = model.data;
                    if (captcha.contains(",")) {
                        imgStr = captcha.substring(captcha.indexOf(',') + 1);
                        imgCode.setImageBitmap(ImageUtil.stringtoBitmap(imgStr));
                    } else imgCode.setImageDrawable(null);
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick({R.id.btn_back, R.id.img_code, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.img_code:
                getChsiImgCodeInfo();
                break;
            case R.id.btn_submit:
                if (codeViewInputCheck()) {
                    try {
                        getRetrieveUsername(UI.toString(editIdno), URLEncoder.encode(editName.getText().toString(), "UTF-8"), URLEncoder.encode(editCode.getText().toString(), "UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }

    private boolean codeViewInputCheck() {
        if (TextUtils.isEmpty(UI.toString(editIdno))) {
            ToastUtil.makeText(getApplicationContext(), "身份证不能为空").show();
            return false;
        }
        if (TextUtils.isEmpty(UI.toString(editName))) {
            ToastUtil.makeText(getApplicationContext(), "姓名不能为空").show();
            return false;
        }
        if (TextUtils.isEmpty(UI.toString(editCode))) {
            ToastUtil.makeText(getApplicationContext(), getString(R.string.code_is_empty)).show();
            return false;
        }
        return true;
    }

}
